﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using CJ_Logging.Configuration.Config;
using CJ_Logging.Configuration;

namespace CJ_Logging
{
    public class SendMail
    {
        private static SendMail m_SendMail = null;
        public static SendMail Instance
        {
            get
            {
                if (m_SendMail == null)
                {
                    m_SendMail = new SendMail();
                }
                return m_SendMail;
            }
        }

        #region 通过STMP发邮件

        public bool SendMailBySmtp(string content)
        {
            return SendMailBySmtp(string.Empty, content);
        }

        public bool SendMailBySmtp(string title, string content)
        {
            return SendMailBySmtp(null,title,content,string.Empty);
        }

        public bool SendMailBySmtp(SendMailConfig newConfig, string content)
        {
            return SendMailBySmtp(newConfig, newConfig.MailTitle, content,string.Empty);
        }

        public bool SendMailBySmtp(string title, string content, string attachmentName)
        {
            return SendMailBySmtp(null, title, content, attachmentName);
        }

        /// <summary>
        /// 通过STMP发邮件
        /// </summary>
        /// <param name="content"></param>
        /// <returns></returns>
        public bool SendMailBySmtp(SendMailConfig newConfig, string title, string content, string attachmentName)
        {
            SendMailConfig config = new SendMailConfig();
            if (newConfig == null)
                config = LoadConfig.Instance.LoadSendMailConfig();
            else
                config = newConfig;

            if (!CheckMailConfig(config))
                return false;

            System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
            msg.To.Add(config.MailTo);
            //msg.CC.Add("c@c.com"); 
            msg.From = new MailAddress(config.MailFrom, config.MailDisplayName, System.Text.Encoding.UTF8);
            /* 上面3个参数分别是发件人地址（可以随便写），发件人姓名，编码*/
            if (string.IsNullOrEmpty(title))
                msg.Subject = config.MailTitle;//邮件标题 
            else
                msg.Subject = title;
            msg.SubjectEncoding = System.Text.Encoding.UTF8;//邮件标题编码 
            msg.Body = content;//邮件内容 
            msg.BodyEncoding = System.Text.Encoding.UTF8;//邮件内容编码 
            msg.IsBodyHtml = config.IsBodyHtml;//是否是HTML邮件 
            msg.Priority = MailPriority.High;//邮件优先级 
            //发送附件
            if (!string.IsNullOrEmpty(attachmentName))
            {
                //string[] files = @"D:\模板文件\积分换购.xls;D:\项目文档\temp.txt".Replace("；", ";").Split(';');
                string[] files = attachmentName.Replace("；", ";").Split(';');
                for (int i = 0; i < files.Length; i++)
                {
                    if (!string.IsNullOrEmpty( files[i]))
                        msg.Attachments.Add(new Attachment(files[i]));
                }
            }

            SmtpClient client = new SmtpClient();
            client.Credentials = new System.Net.NetworkCredential(config.SmtpUserName, config.SmtpPassword);
            client.Host = config.SmtpHost;
            object userState = msg;
            try
            {
                if (config.IsSendAsync)
                    client.SendAsync(msg, userState);//非WEB项目，使用异步
                else
                    client.Send(msg);
                return true;
            }
            catch(Exception e)
            {
                Console.WriteLine(e.Message);
                LogHelper.Instance.WriteError("记录系统日志，自动发邮件出错", e);
                return false;
            }
        }

        #endregion

        #region 验证邮件配置信息的正确性

        private bool CheckMailConfig(SendMailConfig config)
        {
            if (config == null)
                return false;
            if (string.IsNullOrEmpty(config.MailTo))
                return false;
            if (string.IsNullOrEmpty(config.SmtpHost))
                return false;

            return true;
        }

        #endregion

    }
}
